<?

// ********************************
//
//	extracts site struct for viewing
//
// ********************************

require_once E.'_lib/_const.php';

class SECTIONMAP {

	var $parent;
	var $path;
	var $lang;
	var $data;
	
	var $res;
	
	function SECTIONMAP() {

		$this->data = array();
		$this->res = 255;

	}
	
	function init($parent = 0, $path = '', $lang = LANG_DEF, $hook = 'map') {

		$this->parent = $parent;	// start point of site tree
		$this->path = $path;		// result URL prefix
		$this->lang = $lang;		// page name language
		$this->hook = $hook;		// hook for outer code, can be 'map' for user sitemap or 'admin' for admin sitemap
		
		$this->res = $this->fetch();
		return ! (bool) $this->res;
	}
	
	function fetch() {
	
		return ($this->data = $this->_Sectionmap($this->parent, $this->path, $this->lang, $this->hook)) ? false : true;
		
	}



	function _Sectionmap($parent, $path, $lang, $hook) {
	
		$res = array();
	
	//	extract records that:
	//	1. have this parent
	//	2. are visible in Sitemap
	//	3. are accessible by current user, i.e.
	//		PT_ISUSER bit is 0 for ANONYMOUS, 0 or 1 for USER
	
		if ($pages = dataset('db_list', array(
							'table' => tb('struct'),
							'index' => 'id',
							'order' => 'pos',
							'where' => "(parent=$parent) AND
										(".dbAndL('tag', PT_MAPABLE).") AND 
										( (".dbAndI('tag', PT_ISUSER).") OR ".(user_info('priv',0) & PT_ISUSER)."!=0)") )) {

			foreach($pages->data as $id => $pg) {
			
				// if non-def lang or empty page name - extract page name from table 'list'
				if ( ($lang != LANG_DEF) || !$pg['name'] ) 
					if ($nm = dataset('db_item', tb('list'), "(id_list=".L_PAGENAMES.") AND (id_index=$id) AND (assoc='$lang')"))
						$pages->data[$id]['name'] = $nm->data['text'];
				
	
				// check page for hook 'map' to determine it's URL
				if ($code = code($hook, $pg['code'])) {	
					if (is_file("_code/_{$code}_{$hook}.php")) {
						$p = include "_code/_{$code}_{$hook}.php";				// outer code executed

						if (is_string($p)) {								// and returned string - a part of path
							if ($p)
								$pages->data[$id]['url'] = $path.$p.'/';
							else											// if returned empty string
								$pages->data[$id]['url'] = $path;
						} 													// !!! or it returned something else - need 2b checked
					}
				} else {
					$p = $pg['keyword'];										// no support for multiple keywords in one record
																				// due to single Page Name record per Page Id
					$pages->data[$id]['url'] = ($path) ? $path.$p.'/' : $p.'/';
					
				}
				
				$temp = $pages->data[$id];
//				$temp = array(	'url' => $pages->data[$id]['url'], 
//								'name' => $pages->data[$id]['name'],
//							);
	

//				if ($pg['kind'] == PK_REDIRECT) 								// if not real page but redirect - show children !![ some weird code ]
//					$temp = $temp['data'][0];									// usually redirect have ONE child	
	
				$res[$id] = $temp;													// another brick in the wall
			}
	
		}
		
		return (sizeof($res)) ? $res : false;
	}


}


?>